﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Deployment.Application;
using System.Reflection;

namespace MagicCove
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            //Background worker to try and get an http response to speed up card look up
            System.ComponentModel.BackgroundWorker getHTTPResponseBW = new System.ComponentModel.BackgroundWorker();
            getHTTPResponseBW.DoWork += new System.ComponentModel.DoWorkEventHandler(initializeHTTP);
            getHTTPResponseBW.RunWorkerAsync();
            checkForShortcut();
            if (args != null && args.Length != 0) //actual application called with parameter
                Application.Run(new DeckEditorForm(args));
            else if (AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData != null 
                && AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData.Length != 0
                && !AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData[0].EndsWith(".application")) //launched with ClickOnce library, and not the installer being launched
                Application.Run(new DeckEditorForm(AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData));
            else
                Application.Run(new LaunchForm());
        }
        public static bool OptIn
        {
            get
            {
                return (bool)MagicCove.Properties.Settings.Default["OptIn"];
            }
        }
        /// <summary>
        /// This will create a Application Reference file on the users desktop
        /// if they do not already have one when the program is loaded.
        /// </summary
        private static void checkForShortcut()
        {
            try
            {
                if (ApplicationDeployment.CurrentDeployment.IsFirstRun)
                {
                    Assembly code = Assembly.GetExecutingAssembly();

                    string company = string.Empty;
                    string description = string.Empty;

                    if (Attribute.IsDefined(code, typeof(AssemblyCompanyAttribute)))
                    {
                        AssemblyCompanyAttribute ascompany = (System.Reflection.AssemblyCompanyAttribute)Attribute.GetCustomAttribute(code,
                            typeof(AssemblyCompanyAttribute));
                        company = ascompany.Company;
                    }

                    if (Attribute.IsDefined(code, typeof(AssemblyDescriptionAttribute)))
                    {
                        AssemblyDescriptionAttribute asdescription = (AssemblyDescriptionAttribute)Attribute.GetCustomAttribute(code,
                            typeof(AssemblyDescriptionAttribute));
                        description = asdescription.Description;
                    }

                    if (company != string.Empty && description != string.Empty)
                    {
                        string desktopPath = string.Empty;
                        desktopPath = string.Concat(Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
                            "\\", description, ".appref-ms");

                        string shortcutName = string.Empty;
                        shortcutName = string.Concat(Environment.GetFolderPath(Environment.SpecialFolder.Programs),
                            "\\", company, "\\", description, ".appref-ms");

                        System.IO.File.Copy(shortcutName, desktopPath, true);
                    }

                }
            }
            catch { }
        }
        private static void initializeHTTP(object sender, EventArgs e)
        {
            try
            {//use a webpage that should always be running, and is fairly small
                ((System.Net.HttpWebRequest)System.Net.WebRequest.Create("http://www.google.com")).GetResponse();
            }
            catch { }
        }
        public static void newDEThread()
        {
            Application.Run(new DeckEditorForm());
        }
        public static void newHelpThread()
        {
            Application.Run(new HelpForm());
        }
        public static void newGameThread()
        {
            Application.Run(new GameForm(LaunchForm.staticLF.p1DeckO, LaunchForm.staticLF.p2DeckO, LaunchForm.staticLF.enforceRules));
        }
    }
    #region General Global Definitions
    #endregion
    #region Global Enumerations
    public enum Phase { Untap, Upkeep, Draw, PreCombatMain, BeginningOfCombat, DeclareAttackers, DeclareBlockers, CombatDamage, EndOfCombat, PostCombatMain, End, CleanUp };
    public enum TargetLocation { None, TheStack, CurrentPlayersHand, CurrentPlayersField, CurrentPlayersGraveyard, CurrentPlayersExiled, OpponentsHand, OpponentsField, OpponentsGraveyard, OpponentsExiled }; //~!~ can hands be targeted?
    public enum CurrentLocation { Deck, Hand, Stack, Field, Graveyard, Exiled };
    public enum CardRarity { BasicLand, Common, Uncommon, Rare, Mythic, Timeshifted };
    public enum Player { None = -1, P1, P2, P3, P4, P5, P6, P7, P8, P9 }; //note P1 is 0, P2 is 1... (this was done so mods would work out nicely)
    public enum Keyword
    {//Note: A lot of these will never be used
        None, //not actually used, this is usually indicated by the absence in a list
        //Evergreen keywords
        Deathtouch,
        Defender,
        DoubleStrike,
        Enchant,
        Equip,
        FirstStrike,
        Flash,
        Flying,
        Haste,
        Hexproof,
        Intimidate,
        Landwalk,
        Lifelink,
        Protection,
        Reach,
        Shroud,
        Trample,
        Vigilance,
        //Keyword Actions
        Attach,
        Counter,
        Exile,
        Regenerate,
        Sacrifice,
        Tap,
        Untap,
        //Keywords from Expert-Level expansions
        Absorb,
        Affinity,
        Amplify,
        Annihalator,
        AuraSwap,
        BattleCry,
        Bloodthirst,
        Bushido,
        Buyback,
        Cascade,
        Champion,
        Changeling,
        Clash,
        Conspire,
        Convoke,
        CumulativeUpkeep,
        Cycling,
        Delve,
        Devour,
        Dredge,
        Echo,
        Entwine,
        Epic,
        Evoke,
        Exalted,
        Fading,
        Fateseal,
        Flanking,
        Flashbock,
        Forecast,
        Fortify,
        Frenzy,
        Graft,
        Gravestorm,
        Haunt,
        Hideaway,
        Horsemanship,
        Infect,
        Kicker,
        LevelUp,
        LivingWeapon,
        Madness,
        Modular,
        Morph,
        Multikicker,
        Ninjutsu,
        Offering,
        Persist,
        Phasing,
        Poisonous,
        Proliferate,
        Provoke,
        Prowl,
        Rampage,
        Rebound,
        Recover,
        Reinforce,
        Replicate,
        Retrace,
        Ripple,
        Scry,
        Shadow,
        Soulshift,
        Splice,
        SplitSecond,
        Storm,
        Sunburst,
        Suspend,
        TotemArmor,
        Transfigure,
        Transmute,
        Typecycling,
        Unearth,
        Vanishing,
        Wither,
        //Ability Words
        Channel,
        Chroma,
        Domain,
        Grandeur,
        Hellbent,
        Imprint,
        JoinForces,
        Kinship,
        Landfall,
        Metalcraft,
        Radiance,
        Sweep,
        Threshold,
        //Discontinued keywords
        Banding,
        BandsWithOther,
        Bury,
        Fear,
        Landhome,
        Substance

    }
    [Flags]
    public enum ManaType
    {//X mana is dealt by the sE as the X implies a sE must exist that is based on it
        Colorless = 0,
        White = 0x1,
        Blue = 0x2,
        Black = 0x4,
        Red = 0x8,
        Green = 0x10,
        TwoColorless = 0x20, //for the card that are like 1 blue or 2 colorless (also permits smaller lists)
        ThreeColorless = 0x40, //for the card that are like 1 blue or 3 colorless (also permits smaller lists)
        Phyrexian = 0x80 //phyrexian is not actually a color, it is just for mana payment
    }
    [Flags]
    public enum Color
    {
        Colorless = 0,
        White = 0x1,
        Blue = 0x2,
        Black = 0x4,
        Red = 0x8,
        Green = 0x10,
    }
    [Flags]
    enum CardSupertype
    {
        None = 0,
        Basic = 0x1,
        Legendary = 0x2,
        Snow = 0x4,
        World = 0x8,
        Token = 0x10 //this is strictly not correct, this is not a supertype, but allows us internally to distinguish cards. 
    }
    [Flags]
    enum CardType
    {
        None = 0,
        Artifact = 0x1,
        Creature = 0x2,
        Enchantment = 0x4,
        Instant = 0x8,
        Land = 0x10,
        Planeswalker = 0x20,
        Sorcery = 0x40,
        Tribal = 0x80,
        All = 0xFF
    }
    [Flags]
    public enum CardSubtype
    { //this needs to be improved so that subtypes do not overlap ~!~ (for example a human knight is 0x820058 | 0x820064 = 0x82007C which could be confused with Nautilus)
        None = 0x0,
        // Artifact types
        Contraption = 0x10001,
        Equipment = 0x10002,
        Fortification = 0x10003,

        // Enchantment types
        Aura = 0x40001,
        Shrine = 0x40002,

        // Land types
        Desert = 0x100001,
        Forest = 0x100002,
        Island = 0x100003,
        Lair = 0x100004,
        Locus = 0x100005,
        Mine = 0x100006,
        Mountain = 0x100007,
        Plains = 0x100008,
        PowerPlant = 0x100009,
        Swamp = 0x10000a,
        Tower = 0x10000b,
        Urzas = 0x10000c,

        // Planeswalker types
        Ajani = 0x200001,
        Bolas = 0x200002,
        Chandra = 0x200003,
        Elspeth = 0x200004,
        Garruk = 0x200005,
        Jace = 0x200006,
        Liliana = 0x200007,
        Sarkhan = 0x200008,
        Tezzeret = 0x200009,

        // Spell types
        Arcane = 0x480001,
        Trap = 0x480002,

        // Creature types
        Advisor = 0x820001,
        Angel = 0x820002,
        Anteater = 0x820003,
        Antelope = 0x820004,
        Ape = 0x820005,
        Archer = 0x820006,
        Archon = 0x820007,
        Artificer = 0x820008,
        Assassin = 0x820009,
        AssemblyWorker = 0x82000a,
        Atog = 0x82000b,
        Aurochs = 0x82000c,
        Avatar = 0x82000d,
        Badger = 0x82000e,
        Barbarian = 0x82000f,
        Basilisk = 0x820010,
        Bat = 0x820011,
        Bear = 0x820012,
        Beast = 0x820013,
        Beeble = 0x820014,
        Berserker = 0x820015,
        Bird = 0x820016,
        Blinkmoth = 0x820017,
        Boar = 0x820018,
        Bringer = 0x820019,
        Brushwagg = 0x82001a,
        Camarid = 0x82001b,
        Camel = 0x82001c,
        Caribou = 0x82001d,
        Carrier = 0x82001e,
        Cat = 0x82001f,
        Centaur = 0x820020,
        Cephalid = 0x820021,
        Chimera = 0x820022,
        Citizen = 0x820023,
        Cleric = 0x820024,
        Cockatrice = 0x820025,
        Construct = 0x820026,
        Coward = 0x820027,
        Crab = 0x820028,
        Crocodile = 0x820029,
        Cyclops = 0x82002a,
        Dauthi = 0x82002b,
        Demon = 0x82002c,
        Deserter = 0x82002d,
        Devil = 0x82002e,
        Djinn = 0x82002f,
        Dragon = 0x820030,
        Drake = 0x820031,
        Dreadnought = 0x820032,
        Drone = 0x820033,
        Druid = 0x820034,
        Dryad = 0x820035,
        Dwarf = 0x820036,
        Efreet = 0x820037,
        Egg = 0x820038,
        Elder = 0x820039,
        Elemental = 0x82003a,
        Elephant = 0x82003b,
        Elf = 0x82003c,
        Elk = 0x82003d,
        Eye = 0x82003e,
        Faerie = 0x82003f,
        Ferret = 0x820040,
        Fish = 0x820041,
        Flagbearer = 0x820042,
        Fox = 0x820043,
        Frog = 0x820044,
        Fungus = 0x820045,
        Gargoyle = 0x820046,
        Giant = 0x820047,
        Gnome = 0x820048,
        Goat = 0x820049,
        Goblin = 0x82004a,
        Golem = 0x82004b,
        Gorgon = 0x82004c,
        Graveborn = 0x82004d,
        Griffin = 0x82004e,
        Hag = 0x82004f,
        Harpy = 0x820050,
        Hellion = 0x820051,
        Hippo = 0x820052,
        Homarid = 0x820053,
        Homunculus = 0x820054,
        Horror = 0x820055,
        Horse = 0x820056,
        Hound = 0x820057,
        Human = 0x820058,
        Hydra = 0x820059,
        Hyena = 0x82005a,
        Illusion = 0x82005b,
        Imp = 0x82005c,
        Incarnation = 0x82005d,
        Insect = 0x82005e,
        Jellyfish = 0x82005f,
        Juggernaut = 0x820060,
        Kavu = 0x820061,
        Kirin = 0x820062,
        Kithkin = 0x820063,
        Knight = 0x820064,
        Kobold = 0x820065,
        Kor = 0x820066,
        Kraken = 0x820067,
        Lammasu = 0x820068,
        Leech = 0x820069,
        Leviathan = 0x82006a,
        Lhurgoyf = 0x82006b,
        Licid = 0x82006c,
        Lizard = 0x82006d,
        Manticore = 0x82006e,
        Masticore = 0x82006f,
        Mercenary = 0x820070,
        Merfolk = 0x820071,
        Metathran = 0x820072,
        Minion = 0x820073,
        Minotaur = 0x820074,
        Monger = 0x820075,
        Mongoose = 0x820076,
        Monk = 0x820077,
        Moonfolk = 0x820078,
        Mutant = 0x820079,
        Myr = 0x82007a,
        Mystic = 0x82007b,
        Nautilus = 0x82007c,
        Nephilim = 0x82007d,
        Nightmare = 0x82007e,
        Nightstalker = 0x82007f,
        Ninja = 0x820080,
        Noggle = 0x820081,
        Nomad = 0x820082,
        Octopus = 0x820083,
        Ogre = 0x820084,
        Ooze = 0x820085,
        Orb = 0x820086,
        Orc = 0x820087,
        Orgg = 0x820088,
        Ouphe = 0x820089,
        Ox = 0x82008a,
        Oyster = 0x82008b,
        Pegasus = 0x82008c,
        Pentavite = 0x82008d,
        Pest = 0x82008e,
        Phelddagrif = 0x82008f,
        Phoenix = 0x820090,
        Pincher = 0x820091,
        Pirate = 0x820092,
        Plant = 0x820093,
        Prism = 0x820094,
        Rabbit = 0x820095,
        Rat = 0x820096,
        Rebel = 0x820097,
        Reflection = 0x820098,
        Rhino = 0x820099,
        Rigger = 0x82009a,
        Rogue = 0x82009b,
        Salamander = 0x82009c,
        Samurai = 0x82009d,
        Sand = 0x82009e,
        Saproling = 0x82009f,
        Satyr = 0x8200a0,
        Scarecrow = 0x8200a1,
        Scorpion = 0x8200a2,
        Scout = 0x8200a3,
        Serf = 0x8200a4,
        Serpent = 0x8200a5,
        Shade = 0x8200a6,
        Shaman = 0x8200a7,
        Shapeshifter = 0x8200a8,
        Sheep = 0x8200a9,
        Skeleton = 0x8200aa,
        Slith = 0x8200ab,
        Sliver = 0x8200ac,
        Slug = 0x8200ad,
        Snake = 0x8200ae,
        Soldier = 0x8200af,
        Soltari = 0x8200b0,
        Spawn = 0x8200b1,
        Specter = 0x8200b2,
        Spellshaper = 0x8200b3,
        Sphinx = 0x8200b4,
        Spider = 0x8200b5,
        Spike = 0x8200b6,
        Spirit = 0x8200b7,
        Splinter = 0x8200b8,
        Sponge = 0x8200b9,
        Squid = 0x8200ba,
        Squirrel = 0x8200bb,
        Starfish = 0x8200bc,
        Survivor = 0x8200bd,
        Tetravite = 0x8200be,
        Thalakos = 0x8200bf,
        Thopter = 0x8200c0,
        Thrull = 0x8200c1,
        Treefolk = 0x8200c2,
        Triskelavite = 0x8200c3,
        Troll = 0x8200c4,
        Turtle = 0x8200c5,
        Unicorn = 0x8200c6,
        Vampire = 0x8200c7,
        Vedalken = 0x8200c8,
        Viashino = 0x8200c9,
        Volver = 0x8200ca,
        Wall = 0x8200cb,
        Warrior = 0x8200cc,
        Weird = 0x8200cd,
        Whale = 0x8200ce,
        Wizard = 0x8200cf,
        Wolf = 0x8200d0,
        Wolverine = 0x8200d1,
        Wombat = 0x8200d2,
        Worm = 0x8200d3,
        Wraith = 0x8200d4,
        Wurm = 0x8200d5,
        Yeti = 0x8200d6,
        Zombie = 0x8200d7,
        Zubera = 0x8200d8
    }
    #endregion
}
